package GreedyAlgorithmPackage;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @author Lzm
 * @version 1.0
 */
public class candy_ {
  @Test
  public void candyTest(){
    //int[] nums = new int[]{1,2,2};
    //int[] nums = new int[]{1,2,87,87,87,2,1};
    //int[] nums = new int[]{1,0,2};
    int[] nums = new int[]{1,2,3,3,2,1};
    System.out.println(candy(nums));
  }

  public int candy(int[] ratings) {
    int[] candies = new int[ratings.length];
    candies[0] = 1;
    for (int i = 1; i < ratings.length - 1; i++) {
      if (ratings[i] > ratings[i + 1]){
        candies[i] = candies[i - 1] + 1;
      }
      else{
        candies[i] = 1;
      }
    }
    for (int i = ratings.length - 2; i >= 0; i--) {
      if (ratings[i] > ratings[i + 1]) {
        candies[i] = Math.max(candies[i], candies[i + 1] + 1);
      }
    }

    int ans = 0;
    for (int num : candies) {
      ans += num;
    }
    return ans;
  }
  public int getSum(int[] nums){
    int sum = 0;
    for (int num : nums) {
      sum += num;
    }
    return sum;
  }
}
